//
//  LDBossFinderResumeNameCell.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/6/8.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit
import SDWebImage

class LDBossFinderResumeNameCell: LDBaseRecFinderCell {
    
    var nameLab:UILabel?
    var workStatus:UILabel?
    var headImageView:UIImageView?
    
    var genderBtn:UIButton?
    var cityBtn:UIButton?
    var experienceBtn:UIButton?
    var eduBtn:UIButton?
    
    var zhaoPic:UIImageView?
    var jobName:UILabel?
    var expectSalary:UILabel?
    
    var expectBtn:UIButton?
    var expectTag:UILabel?
    var models = LDBossFinderResumeNameCellModel() {
        
        didSet{
            updateFrames()
        }
    }
    
    var divideLine1 = UIImageView.setupDivideLine_H(LDFrame.ScreenWidth - Margin_V * 8)
    
    
    let imgHeadW:CGFloat = IPHONE6UP ? 50:40
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setDefaultContent()
        setupFrame()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    func setDefaultContent() {
        
        nameLab = UILabel.setDetailsLab("Name", fontSize: 16, titleColor: LDFontColor.Green,Alignment: nil)
        workStatus = UILabel.setDetailsLab("", fontSize: 13, titleColor: LDFontColor.LightGray,Alignment: .Left)
        headImageView = UIImageView.setDefaultGoundImg("default_header", radius: imgHeadW/2)
        genderBtn = UIButton.setDetailsBtn("女", picName: "女.png", btnBackColor: LDFontColor.White, titleColor: LDFontColor.DeepGray)
        genderBtn?.titleLabel?.font = LDFont.Font13
        genderBtn?.contentMode = UIViewContentMode.Left
        
        genderBtn?.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 0)
        
        cityBtn = UIButton.setLoactionBtn()
        experienceBtn = UIButton.setExperienceBtn()
        eduBtn = UIButton.setEduBtn()
        
        jobName = UILabel.setDetailsLab("视觉设计师", fontSize: 16, titleColor: LDFontColor.Green,Alignment: nil)
        expectSalary = UILabel.setDetailsLab("￥ 10K-10K", fontSize: 14, titleColor: LDFontColor.Red,Alignment: nil)
        expectBtn = UIButton.setDetailsBtn("期望行业 ", picName: "期望行业", btnBackColor: LDGolbalColor.White, titleColor: LDFontColor.DeepGray)
        expectBtn?.titleLabel?.font = LDFont.Font14
        expectTag = UILabel.setDetailsLab("", fontSize: 14, titleColor: LDFontColor.DeepGray,Alignment: .Left)
        
        gapView.addSubview(nameLab!)
        gapView.addSubview(workStatus!)
        gapView.addSubview(headImageView!)
        gapView.addSubview(genderBtn!)
        gapView.addSubview(cityBtn!)
        gapView.addSubview(experienceBtn!)
        gapView.addSubview(eduBtn!)
        gapView.addSubview(jobName!)
        gapView.addSubview(expectSalary!)
        gapView.addSubview(expectBtn!)
        gapView.addSubview(expectTag!)
        
        gapView.addSubview(divideLine1)
    }
    
    func updateFrames() {
        
        nameLab?.text = models.name
        workStatus?.text = models.status
        let urlStr = LDCommentAPI.getPicApi(models.logo)
        
        headImageView?.sd_setImageWithURL(NSURL(string: urlStr), placeholderImage: UIImage(named: "default_header"))
        
        if models.sex == "男" {
            genderBtn?.setTitle(models.sex, forState: .Normal)
            genderBtn?.setImage(UIImage.init(named: "男"), forState: .Normal)
        } else if models.sex == "女" {
            genderBtn?.setTitle(models.sex, forState: .Normal)
            genderBtn?.setImage(UIImage(named: "女"), forState: .Normal)
        } else {
            genderBtn?.setTitle("女", forState: .Normal)
            genderBtn?.setImage(UIImage(named: "女"), forState: .Normal)
        }
        
        cityBtn?.setTitle(models.expectcity, forState: .Normal)
        experienceBtn?.setTitle(models.experience, forState: .Normal)
        
        
        if !(models.edu.characters.count > 0) {
            models.edu = "学历"
        }
        eduBtn?.setTitle(models.edu, forState: .Normal)
        jobName?.text = models.expectjobcategory
        
        if models.expectsalary.characters.count > 0 {
            
            expectSalary?.text = "￥ \(models.expectsalary)"
        } else {
            
            expectSalary?.text = "期望薪资未知"
        }
        
        var strtilte = String()
        
        for item in models.expectjobindustries {
            
            let str = item
            strtilte.appendContentsOf(" | \(str)")
        }
        
        expectTag?.text = strtilte
        dispatch_async(dispatch_get_global_queue(0, 0)) { [weak self] in
            let textsize = CGSizeMake(120, 15)
            
            let size1 = self!.models.expectcity.boundingRectWithSize(textsize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font13], context: nil)
            let size2 = self!.models.experience.boundingRectWithSize(textsize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font13], context: nil)
            let size3 = self!.models.edu.boundingRectWithSize(textsize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font13], context: nil)
            
            let textsizeName = CGSizeMake(200, 25)
            let nameTitle = self!.models.name.boundingRectWithSize(textsizeName, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:LDFont.Font16], context: nil)
            
            
            dispatch_async(dispatch_get_main_queue(), {
                
                self!.nameLab?.snp_updateConstraints(closure: { (make) in
                    make.width.equalTo(nameTitle.size.width + LDMargin_V*2)
                })
                self!.cityBtn!.snp_updateConstraints { (make) in
                    make.width.equalTo(size1.size.width + LDMargin_V*6)
                }
                self!.experienceBtn!.snp_updateConstraints { (make) in
                    make.width.equalTo(size2.size.width + LDMargin_V*6)
                }
                self!.eduBtn!.snp_updateConstraints { (make) in
                    make.width.equalTo(size3.size.width + LDMargin_V*6)
                }
            })
        }
    }
    
    func setupFrame() {
        
        nameLab?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(gapView.snp_top).offset(Margin_V*3)//(Margin_V*3)
            make.left.equalTo(Margin_V*3)
            make.size.equalTo(CGSizeMake(0, 25))
        })
        workStatus?.snp_makeConstraints(closure: { (make) in
            make.left.equalTo((nameLab?.snp_right)!).offset(Margin_V)
            make.centerY.equalTo(nameLab!)
            make.size.equalTo(CGSizeMake(120, 20))
            
        })
        headImageView?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo(Margin_V*1.5)
            make.right.equalTo(-Margin_V*2)
            make.width.height.equalTo(imgHeadW)
        })
        
        
        genderBtn?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo((nameLab?.snp_bottom)!).offset(Margin_V*2)
            make.left.equalTo(nameLab!).offset(0)
            make.width.equalTo(35)
            make.height.equalTo(15)
        })
        cityBtn?.snp_makeConstraints(closure: { (make) in
            make.left.equalTo((genderBtn?.snp_right)!).offset(3)
            make.top.height.equalTo(genderBtn!)
        })
        experienceBtn?.snp_makeConstraints(closure: { (make) in
            make.left.equalTo((cityBtn?.snp_right)!).offset(3)
            make.top.height.equalTo(genderBtn!)
        })
        eduBtn?.snp_makeConstraints(closure: { (make) in
            make.left.equalTo((experienceBtn?.snp_right)!).offset(3)
            make.top.height.equalTo(genderBtn!)
        })
        
        
        divideLine1.snp_makeConstraints { (make) in
            make.top.equalTo((genderBtn?.snp_bottom)!).offset(Margin_V*3)
            make.left.equalTo(Margin_V*2)
            make.right.equalTo(-Margin_V*2)
            make.height.equalTo(1)
        }
        jobName?.snp_makeConstraints(closure: { (make) in
            make.left.equalTo(nameLab!)
            make.top.equalTo(divideLine1.snp_bottom).offset(Margin_V*2)
            //            make.size.equalTo(CGSizeMake(80, 25))
            make.height.equalTo(25)
        })
        expectSalary?.snp_makeConstraints(closure: { (make) in
            make.centerY.equalTo(jobName!)
            make.left.equalTo((jobName?.snp_right)!).offset(Margin_V*2)
            make.size.equalTo(CGSizeMake(140, 20))
        })
        expectBtn?.snp_makeConstraints(closure: { (make) in
            make.top.equalTo((jobName?.snp_bottom)!).offset(Margin_V)
            make.left.equalTo(nameLab!)
            //            make.size.equalTo(CGSizeMake(80, 28))
            make.width.equalTo(80)
        })
        expectTag?.snp_makeConstraints(closure: { (make) in
            make.top.height.equalTo(expectBtn!)
            make.left.equalTo((expectBtn?.snp_right)!)
            make.size.equalTo(CGSizeMake(300, 25))
        })
    }
}
class LDBossFinderResumeNameCellModel:NSObject {
    
    var height:CGFloat = 165
    //models数据
    var name = "" // 姓名
    var sex = "" // 性别
    var experience = "" // 工作经验年限
    var edu = ""// 学历
    var logo = ""
    var expectjobcategory = "" // 期望工作类别
    var expectcity = ""        // 期望工作城市
    var expectsalary = "" // 期望工作薪资
    var status = ""                 // 当前状态
    var jobtitle = ""
    var expectjobindustriescount = 0       // 期望工作所属行业数目
    var expectjobindustries = [AnyObject]()      // 期望工作所属行业数组
}
